package com.gitee.wsl.struct.grammar.ext

import com.gitee.wsl.struct.grammar.GrammarContext
import com.gitee.wsl.struct.grammar.parser.Parser
import com.gitee.wsl.struct.grammar.parser.combinator.leftAssociative
import com.gitee.wsl.struct.grammar.parser.combinator.rightAssociative


inline fun <T : Any, S : Any> GrammarContext.leftAssociative(
    term: Parser<T>,
    separator: Parser<S>,
    crossinline transform: (T, S, T) -> T
): Parser<T> = term.leftAssociative(separator,transform)
/*Parser.parser(term.firstTokens) {
    reduce(term, separator, transform)
}*/

inline fun <T : Any, S : Any> GrammarContext.leftAssociative(
    term: Parser<T>,
    separator: Parser<S>,
    crossinline transform: (T, T) -> T
): Parser<T> = term.leftAssociative(separator) { a, _, b -> transform(a, b) }
//leftAssociative(term, separator) { a, _, b -> transform(a, b) }

inline fun <T : Any, S : Any> GrammarContext.rightAssociative(
    term: Parser<T>,
    separator: Parser<S>,
    crossinline transform: (T, S, T) -> T
): Parser<T> = term.rightAssociative(separator,transform)
/*Parser.parser(term.firstTokens) {
    reduceRight(term, separator, transform)
}*/

inline fun <T : Any, S : Any> GrammarContext.rightAssociative(
    term: Parser<T>,
    separator: Parser<S>,
    crossinline transform: (T, T) -> T
): Parser<T> = term.rightAssociative(separator) { a, _, b -> transform(a, b) }
//rightAssociative(term, separator) { a, _, b -> transform(a, b) }
